package commerce.action;

import java.util.List;

import util.PageModel;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

import commerce.business.CommerceBusiness;
import commerce.hibernate.po.*;

public class ProductAction extends ActionSupport{
	private CommerceBusiness cb = new CommerceBusiness();
	private List productList;
	private int productCount;
	private Product product;
	private String productId;
	private String[] selectedids;
	private List categories;
	public List getCategories() {
		return categories;
	}

	public void setCategories(List categories) {
		this.categories = categories;
	}

	private PageModel pageModel;

	public String[] getSelectedids() {
		return selectedids;
	}

	public void setSelectedids(String[] selectedids) {
		this.selectedids = selectedids;
	}

	public PageModel getPageModel() {
		if(pageModel == null) {
			pageModel = new PageModel();
		}
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public List getProductList() {
		return productList;
	}

	public void setProductList(List productList) {
		this.productList = productList;
	}

	public int getProductCount() {
		return productCount;
	}

	public void setProductCount(int productCount) {
		this.productCount = productCount;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}
	public String edit()
	{
		view();//读取信息
		return "edit";
	}
	public String saveedit() {
		if(getProduct() == null ) {
			addActionError(getText("SAVE_FAILED"));
			return "save";
		}
		
		if(cb.saveProduct(product)) {
			addActionMessage(getText("SAVE_SUCCESS"));
		}
		else
		{
			addActionError(getText("SAVE_FAILED"));
		}
		return "save";
	}
	
	public String list() {
//		if(pageModel != null && pageModel.getPageNo() > 0) {	// 分页
//			productList = cb.getAllProduct(pageModel.getPageNo(), pageModel.getPageSize());
//			pageModel.setTotalRecords(cb.getAllProduct().size());
//		} else {	// 不分页
//			productList = cb.getAllProduct();
//		}
		// 默认分页
		getPageModel();
		productList = cb.getAllProduct(pageModel.getPageNo(), pageModel.getPageSize());
		pageModel.setTotalRecords(cb.getAllProduct().size());
		
		productCount = 0;
		if(productList != null) {
			productCount = productList.size();
		}
		return "list";
	}
	
	public String delete() {
		if(getProductId() == null || "".equals(getProductId())) {
			addActionError(getText("DELETE_FAILED"));
			return "afterdelete";
		} else {
			product = cb.getProductById(Integer.parseInt(this.getProductId()));
			if(product != null) {
				if(cb.deleteProduct(product)==true)
				{
					addActionMessage(getText("DELETE_SUCCESS"));
				}
				else
				{
					addActionError(getText("DELETE_FAILED"));
				}
				
			} else {
				addActionError(getText("DELETE_FAILED"));
			}
		}
		return "afterdelete";
	}
	
	public String deletebatch() {
		if(selectedids == null || selectedids.length ==0) {
			addActionError(getText("DELETE_FAILED"));
			return "delete";
		} else {
			Integer[] ids = new Integer[selectedids.length];
			for(int i=0; i<ids.length; i++) {
				ids[i] = Integer.parseInt(selectedids[i]);
			}
			if(cb.deleteProductBatch(ids)==true)
			{
				addActionMessage(getText("DELETE_SUCCESS"));
			}
			else
			{
				addActionError(getText("DELETE_FAILED"));
			}
		}
		return "delete";
	}
	
	public String view() {
		if(getProductId() == null || "".equals(getProductId())) {
			product = new Product();
		} else {
			product = cb.getProductById(Integer.parseInt(getProductId()));
		} 
		this.categories=cb.getAllParentCategroy();
		return "view";
	}
	
	
	
}
